package com.hk.devp.captcha.web;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.hk.devp.captcha.utils.CaptchaUtils;

@Controller
@RequestMapping("/captcha")
public class CaptchaController {
	
	/**
	* @author: dragon0574@sina.com
	* @date 2017年7月28日 下午3:02:09 
	* @param request
	* @param response
	* @throws ServletException
	* @throws IOException 
	* @Description:获取技术题的验证码
	 */
	@RequestMapping("/getCalCaptchaImage")
	public void getCalCaptchaImage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		CaptchaUtils.genCalCaptchaImage(request, response);
	}
	
	/**
	* @author: dragon0574@sina.com
	* @date 2017年7月28日 下午3:02:25 
	* @param request
	* @param response
	* @throws ServletException
	* @throws IOException 
	* @Description:获取普通验证码
	 */
	@RequestMapping("/getSimpleCaptchaImage")
	public void getSimpleCaptchaImage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		CaptchaUtils.genSimpleCaptchaImage(request, response);
	}
	
	/**
	* @author: dragon0574@sina.com
	* @date 2017年7月28日 下午3:02:42 
	* @param request
	* @param response
	* @throws ServletException
	* @throws IOException 
	* @Description:获取汉字验证码
	 */
	@RequestMapping("/getChineseCaptchaImage")
	public void getChineseCaptchaImage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		CaptchaUtils.genChineseCaptchaImage(request, response);
	}
	
	/**
	* @author: dragon0574@sina.com
	* @date 2017年7月28日 下午3:03:00 
	* @param request
	* @param response
	* @throws ServletException
	* @throws IOException 
	* @Description:获取谷歌开源的验证码
	 */
	@RequestMapping("/getKCaptchaImage")
	public void getKCaptchaImage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		
		HttpSession session = request.getSession();
		
		response.setDateHeader("Expires", 0);  
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");  
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");  
		response.setHeader("Pragma", "no-cache");  
		response.setContentType("image/jpeg"); 
		
		InputStream resourceAsStream = this.getClass().getResourceAsStream("/captcha.conf");
		Properties properties=new Properties();
		properties.load(resourceAsStream);
		Config config=new Config(properties);
		DefaultKaptcha producer=new DefaultKaptcha();
		producer.setConfig(config);
		
		//生成验证码文本
		String capText = producer.createText();  
		session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
		System.out.println("生成验证码文本===="+capText);
		//利用生成的字符串构建图片
		BufferedImage bi = producer.createImage(capText);
		ServletOutputStream out = response.getOutputStream();  
		ImageIO.write(bi, "jpg", out);  
		
		try {  
		    out.flush();  
		} finally {  
		    out.close();  
		}
	}
	
	
	
	
	
	
	
	
	
}
